import javazoom.jl.player.*;
import javazoom.jl.decoder.*;
import java.io.*;

public class MP3Player {

    public Player player;
    private InputStream stream;
    public Boolean paused;
 
    public MP3Player(File chosenFile) 
    {
	if (chosenFile != null){
	    try
		{
		    stream = new FileInputStream(chosenFile);
		}
	    catch(Exception e){}
	}
	else {
	    try
		{
		    stream = new FileInputStream("test.mp3");
		}
	    catch(Exception e){}
	}
	try {
	    player = new Player(stream);
	}
	catch(Exception E) {}
	paused = true;
    }

   public MP3Player(InputStream is) 
    {
	if (is != null){
	    try
		{
		    stream = is;
		}
	    catch(Exception e){}
	}
	else {
	    try
		{
		    stream = new FileInputStream("test.mp3");
		}
	    catch(Exception e){}
	}
	try {
	    player = new Player(stream);
	}
	catch(Exception E){}
	paused = true;
    }


    public void play()
    {
	paused = false;
	try {
	    InnerPlayerThread innerThread = new InnerPlayerThread();
	    innerThread.start();
	    while(!player.isComplete())
		{
		    if (this.paused){
			player.playing = false;
		    }
		    else {
			int position = player.getPosition();
			System.out.println("Position: " + position);
			Thread.sleep(1000);
		    }
		}
	}
	catch(Exception e) {}
    }
 
    class InnerPlayerThread extends Thread
    {
	public void run()
	{
	    try
		{
		    player.play();
		}
	    catch(Exception e){}
	}
    }
}